package com.copera.cn.kaka;

import org.json.JSONObject;

import android.content.pm.ActivityInfo;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.ActivityManager;
import com.copera.cn.kaka.util.HttpUtils;
import com.copera.cn.kaka.vo.ShopVo;
/**
 * @Description: 卡点有效期
 *
 * @author lyc
 *
 * @date 2014年8月7日
 */
public class TrackValidActivity extends BaseActivity{
	
	private Button goBack;
	private RelativeLayout month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12;
	private ImageView img1,img2,img3,img4,img5,img6,img7,img8,img9,img10,img11,img12,img;
	private String month;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityManager.getInstance().push(this);
		setContentView(R.layout.track_valid);
		
		goBack = (Button)findViewById(R.id.bt_img);
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		initViews();
		initValue();
		initEvent();
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {

				case ActionUrls.FAILURE:
					Toast.makeText(TrackValidActivity.this, (String) msg.obj,
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};
	}
	private void initViews(){
		month1 = (RelativeLayout) findViewById(R.id.rl_06);
		month2 = (RelativeLayout) findViewById(R.id.rl_08);
		month3 = (RelativeLayout) findViewById(R.id.rl_10);
		month4 = (RelativeLayout) findViewById(R.id.rl_12);
		month5 = (RelativeLayout) findViewById(R.id.rl_16);
		month6 = (RelativeLayout) findViewById(R.id.rl_18);
		month7 = (RelativeLayout) findViewById(R.id.rl_20);
		month8 = (RelativeLayout) findViewById(R.id.rl_22);
		month9 = (RelativeLayout) findViewById(R.id.rl_24);
		month10 = (RelativeLayout) findViewById(R.id.rl_26);
		month11 = (RelativeLayout) findViewById(R.id.rl_28);
		month12 = (RelativeLayout) findViewById(R.id.rl_30);
		img1 = (ImageView) findViewById(R.id.img_01);
		img2 = (ImageView) findViewById(R.id.img_02);
		img3 = (ImageView) findViewById(R.id.img_03);
		img4 = (ImageView) findViewById(R.id.img_04);
		img5 = (ImageView) findViewById(R.id.img_05);
		img6 = (ImageView) findViewById(R.id.img_06);
		img7 = (ImageView) findViewById(R.id.img_07);
		img8 = (ImageView) findViewById(R.id.img_08);
		img9 = (ImageView) findViewById(R.id.img_09);
		img10 = (ImageView) findViewById(R.id.img_10);
		img11 = (ImageView) findViewById(R.id.img_11);
		img12 = (ImageView) findViewById(R.id.img_12);
	}
	private void initValue(){
		ShopVo shop = getAppCustomer().getShop();
		month = shop.getValidDate();
		if (!"0".equals(month)) {
			if ("1".equals(month)) {
				img = img1;
			}else if ("2".equals(month)) {
				img = img2;
			}else if ("3".equals(month)) {
				img = img3;
			}else if ("4".equals(month)) {
				img = img4;
			}else if ("5".equals(month)) {
				img = img5;
			}else if ("6".equals(month)) {
				img = img6;
			}else if ("7".equals(month)) {
				img = img7;
			}else if ("8".equals(month)) {
				img = img8;
			}else if ("9".equals(month)) {
				img = img9;
			}else if ("10".equals(month)) {
				img = img10;
			}else if ("11".equals(month)) {
				img = img11;
			}else {
				img = img12;
			}
			img.setVisibility(View.VISIBLE);
		}
		
	}
	private void initEvent(){
		goBack.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		//1个月
		month1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img1.setVisibility(View.VISIBLE);
				img = img1;
				month = "1";
				setValid();
			}
		});
		//2个月
		month2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img2.setVisibility(View.VISIBLE);
				img = img2;
				month = "2";
				setValid();
			}
		});
		//3个月
		month3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img3.setVisibility(View.VISIBLE);
				img = img3;
				month = "3";
				setValid();
			}
		});
		//4个月
		month4.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img4.setVisibility(View.VISIBLE);
				img = img4;
				month = "4";
				setValid();
			}
		});
		//5个月
		month5.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img5.setVisibility(View.VISIBLE);
				img = img5;
				month = "5";
				setValid();
			}
		});	
		month6.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img6.setVisibility(View.VISIBLE);
				img = img6;
				month = "6";
				setValid();
			}
		});	
		month7.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img7.setVisibility(View.VISIBLE);
				img = img7;
				month = "7";
				setValid();
			}
		});	
		month8.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img8.setVisibility(View.VISIBLE);
				img = img8;
				month = "8";
				setValid();
			}
		});	
		month9.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img9.setVisibility(View.VISIBLE);
				img = img9;
				month = "9";
				setValid();
			}
		});	
		month10.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img10.setVisibility(View.VISIBLE);
				img = img10;
				month = "10";
				setValid();
			}
		});	
		month11.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img11.setVisibility(View.VISIBLE);
				img = img11;
				month = "11";
				setValid();
			}
		});	
		month12.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (img != null) {
					img.setVisibility(View.GONE);
				}
				img12.setVisibility(View.VISIBLE);
				img = img12;
				month = "12";
				setValid();
			}
		});	
	}
	private void setValid(){
		new AsyncTask<String, Integer, BitmapDrawable>() {
  			@Override
  			protected  BitmapDrawable doInBackground(String... params) {
  				BitmapDrawable bd;
  				try {
  					ShopVo shop = getAppCustomer().getShop();
  					String url = ActionUrls.URL_SHOP_VALID + "?shopId="+shop.getShopId()+"&validDate="+month;
  					String json = HttpUtils.get(url, getAppCustomer().getHttpClient());
  					JSONObject jo = new JSONObject(json);
  					String message = jo.getString("message");
  					Message msg = handler.obtainMessage(ActionUrls.FAILURE, message);
					handler.sendMessage(msg);
					if ("设置成功".equals(message)) {
						getAppCustomer().getShop().setValidDate(month);
					}
					finish();
					return null;
  				} catch (Exception e) {
  					e.printStackTrace();
  					return null;
  				}
  			}
  		}.execute("");
	}
}
